﻿@inherits ProComponentBase
@inject NavHelper NavHelper
@inject GlobalConfig GlobalConfig
@implements IDisposable

<MNavigationDrawer @bind-Value="Visible" 
                   Class="@($"navigation {ComputedNavigationClass}")"
                   Width=300 
                   MiniVariantWidth=80
                   Floating
                   ExpandOnHover="GlobalConfig.ExpandOnHover"
                   MiniVariant="GlobalConfig.ExpandOnHover"
                   App>

    <div class="logo" @onclick="() => NavHelper.NavigateTo(GlobalVariables.DefaultRoute)">
        <img class="hover-pointer" height="49" src="/img/mainLayout/logo-en.svg" />
    </div>

    <MList Nav Routable Dense Class="@($"pa-0 {(GlobalConfig.ExpandOnHover ? "mini-navigation" : "")}")">

        @foreach (var nav in NavHelper.Navs)
        {
            if (nav.Children is null)
            {
                <MBorder Value=false Offset Width=4 Class="rounded-r-1" Border="Borders.Right">
                    <MListItem Ripple=false Class="mb-4" ActiveClass="fill-lighten-1" Href="@nav.Href">
                        <ItemContent>
                            <MListItemIcon>
                                <MIcon Color="@(context.Active ? "primary" : "neutral-lighten-2")" Size=20>@nav.Icon</MIcon>
                            </MListItemIcon>
                            <MListItemContent>
                                <div style="margin-left:6.5px;" class="text-truncate white-space:nowrap @(context.Active ? "text-subtitle" : "text-body neutral-lighten-2--text")">@T(nav.Title)</div>
                            </MListItemContent>
                        </ItemContent>
                    </MListItem>
                </MBorder>
            }
            else
            {
                <MListGroup Group="@nav.Children.Select(n => n.Href).ToList()" Class="mb-4" PrependIcon="@nav.Icon" NoAction ActiveClass="primaryText" AppendIcon="M8.33398 10.3335L12.5007 14.5002L16.6673 10.3335H8.33398Z">
                    <ActivatorContent>
                        <MListItemContent>
                            <div style="margin-left:6.5px;" class="text-truncate white-space:nowrap">@T(nav.Title)</div>
                        </MListItemContent>
                    </ActivatorContent>
                    <ChildContent>
                        @foreach (var navChild in nav.Children)
                        {
                            <MBorder Value=false Offset Width=4 Class="rounded-r-1" Border="Borders.Right">
                                <MListItem Ripple=false ActiveClass="fill-lighten-1" Link Href="@navChild.Href">
                                    <ItemContent>
                                        <MListItemContent>
                                            <div class="text-truncate white-space:nowrap @(context.Active ? "text-subtitle2" : "text-btn neutral-lighten-2--text")">@T(navChild.Title)</div>
                                        </MListItemContent>
                                    </ItemContent>
                                </MListItem>
                            </MBorder>
                        }
                    </ChildContent>
                </MListGroup>
            }
        }
    </MList>
</MNavigationDrawer>

@if (!(Masa.Breakpoint.Mobile && Visible is true))
{
    <MButton Small Fab Class="nav-toggle-btn" Color="primary" OnClick="Switch">
        <MIcon Class="white--text">
            @(Visible is false || GlobalConfig.ExpandOnHover ? "mdi-chevron-right" : "mdi-chevron-left")
        </MIcon>
    </MButton>
}

@code {

    [Inject]
    public MasaBlazor Masa { get; set; } = default!;

    public bool? Visible { get; set; } = true;

    public string ComputedNavigationClass => (GlobalConfig.NavigationStyle == NavigationStyles.Rounded ? "rounded-r-xl" : string.Empty);

    protected override void OnInitialized()
    {
        base.OnInitialized();

        GlobalConfig.NavigationStyleChanged += NavigationStyleChanged;
    }

    private void NavigationStyleChanged(object? sender, EventArgs e)
    {
        InvokeAsync(StateHasChanged);
    }

    public void Switch()
    {
        if (Visible is true)
        {
            GlobalConfig.ExpandOnHover = !GlobalConfig.ExpandOnHover;
        }
        else
        {
            GlobalConfig.ExpandOnHover = false;
            Visible = true;
        }
    }

    void IDisposable.Dispose()
    {
        GlobalConfig.NavigationStyleChanged -= NavigationStyleChanged;
    }

}
